package coms.fourthirty.emm;

import java.util.ArrayList;

import coms.fourthirty.emm.resources.EMSResources;
import coms.fourthirty.emm.resources.Message;
import coms.fourthirty.emm.resources.WorkSession;
import coms.fourthirty.emm.serverproxy.EMSServerProxy;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class EMSLogin extends Activity {
	private ProgressDialog progress;
	private ArrayList<Message> msgs = new ArrayList<Message>();
	private String username;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle grundle) {
		super.onCreate(grundle);
		setContentView(R.layout.login);
	}

	public void logIn(View view) {

		progress = ProgressDialog
				.show(this, null, "Wait for it...", true, true);

		final Handler handler = new Handler() {
			@Override
			public void handleMessage(android.os.Message msg) {
				progress.dismiss();
				Intent i = new Intent(EMSLogin.this, EMSHome.class);
				// i.putExtra("coms.fourthirty.emm.username", username);
				// i.putExtra("coms.fourthirty.emm.msgs", msgs);
				EMSResources.username = username;
				EMSResources.msgs = msgs;
				startActivity(i);
			}
		};

		new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(2000);
					if (attemptLogin(((EditText) findViewById(R.id.username))
							.getText().toString(),
							((EditText) findViewById(R.id.password)).getText()
									.toString())) {
						downloadMensajes();
						downloadSchedule();
						downloadTimesheet();
						handler.sendEmptyMessage(0);
					} else {
						Toast.makeText(getBaseContext(),
								"Incorrect username/password combination",
								Toast.LENGTH_LONG).show();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();

		new Thread(new Runnable() {
			public void run() {
				try {
					Thread.sleep(30 * 1000);
					EMSLogin.this.notify(0);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	private void notify(int n) {
		// TODO: remove - demo code
		if (!EMSResources.msgs.get(0).from.equals("geoff"))
			EMSResources.msgs.add(0, new Message("geoff", username,
					"You're Fired", "Pack up and go."));

		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
		int icon = R.drawable.ic_ish64;
		CharSequence tickerText = "New Message";
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);

		Context context = getApplicationContext();
		CharSequence contentTitle = "EMS";
		CharSequence contentText = "New Message in Inbox";
		Intent notificationIntent = new Intent(EMSLogin.this, EMSMessages.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText,
				contentIntent);

		mNotificationManager.notify(EMSResources.MESSAGES_NOTIF, notification);
	}

	private boolean attemptLogin(String u, String p) {
		// TODO: server
		username = ((EditText) findViewById(R.id.username)).getText()
				.toString();
		String password = ((EditText) findViewById(R.id.password)).getText()
				.toString(); 
		return EMSServerProxy.login(username, password);
	}

	private void downloadMensajes() {
		msgs = EMSServerProxy.getMessages(username);
	}

	private void downloadSchedule() {
		// TODO: server
		ArrayList<WorkSession> ret = new ArrayList<WorkSession>();

		ret.add(new WorkSession());
		ret.add(new WorkSession());
		ret.add(new WorkSession());
		ret.add(new WorkSession());
		ret.add(new WorkSession());
		ret.add(new WorkSession());

		if (EMSResources.schedule.size() < ret.size())
			EMSResources.schedule.addAll(ret);
	}

	private void downloadTimesheet() {
		// TODO
	}
}